package threaddemo;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class CachedThreadPoolDemo {


    public static void main(String[] args) {

        Integer size = 10;

        CountDownLatch countDownLatch = new CountDownLatch(size);

        //ExecutorService pool = Executors.newCachedThreadPool();
//        ExecutorService pool = Executors.newFixedThreadPool(5);
        ExecutorService pool = Executors.newSingleThreadExecutor();
        for (int i = 0; i < size; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "线程调用了");
                    countDownLatch.countDown();
                }
            });
        }
        try {
            countDownLatch.await();
            pool.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
    }

}
